package org.darkforest.admin.controller;

import org.darkforest.common.services.StatOrderGoodsService;
import org.darkforest.common.util.CsvUtil;
import org.darkforest.dao.mapper.StoreMapper;
import org.darkforest.model.OrderStatistics;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.model.GoodsStatistical;
import org.darkforest.model.Store;
import org.darkforest.vo.MemberConsumptionStatisticVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by valentina on 15/4/29.
 * 统计计算控制类
 */
@Controller
@RequestMapping("stat")
public class StatController {

    @Autowired
    private StatOrderGoodsService statOrderGoodsService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private StoreMapper storeMapper;

    /**
     * 统计概览
     * @param request
     * @return
     */
    @RequestMapping("statisticsList")
    public ModelAndView main(HttpServletRequest request) {
        ModelAndView res = new ModelAndView("stat/statisticsList");
        Integer searchDay = 30;
        String searchScope = request.getParameter("searchScope");
        if (searchScope != null && !"".equals(searchScope)) {
            searchDay = Integer.valueOf(searchScope);
        }
        String storeId = request.getParameter("storeId");
        //查询所有店铺信息
        Store store = new Store();
        store.setPageSwitch(false);
        List<Store> storeList = storeMapper.findStoreByCondition(store);

        OrderStatistics orderAmount = new OrderStatistics();
        OrderStatistics orderAmountLimit = new OrderStatistics();
        OrderStatistics goodsCount = new OrderStatistics();
        List<OrderStatistics> orderStatisticsList = new ArrayList<OrderStatistics>();
        if (storeId != null && !"".equals(storeId)) {
            orderAmount = statOrderGoodsService.getOrderAmount(Integer.valueOf(storeId));
            orderAmountLimit = statOrderGoodsService.getOrderAmountLimit(searchDay, Integer.valueOf(storeId));
            goodsCount = statOrderGoodsService.getGoodsCount(Integer.valueOf(storeId));
            //取得销售走势数据
            orderStatisticsList = statOrderGoodsService.statisticOrder(searchDay, Integer.valueOf(storeId));
        } else {
            orderAmount = statOrderGoodsService.getAllOrderAmount();
            orderAmountLimit = statOrderGoodsService.getAllOrderAmountLimit(searchDay);
            goodsCount = statOrderGoodsService.getAllGoodsCount();
            //取得销售走势数据
            orderStatisticsList = statOrderGoodsService.statisticAllOrder(searchDay);
        }
        if (orderAmount == null) {
            orderAmount = new OrderStatistics();
            orderAmount.setsOrderAmount(new BigDecimal(0));
        }
        if (orderAmountLimit == null) {
            orderAmountLimit = new OrderStatistics();
            orderAmountLimit.setsOrderAmount(new BigDecimal(0));
            orderAmountLimit.setsOrderMemberCount(0);
        }
        if (goodsCount == null) {
            goodsCount = new OrderStatistics();
            goodsCount.setsAllGoosCount(0);
            goodsCount.setsInSaleCount(0);
            goodsCount.setsOutSaleCount(0);
        }

        //日期
        List<String> dates = new ArrayList<String>();
        //销量
        List<Float> sales = new ArrayList<Float>();
        for (OrderStatistics tmp : orderStatisticsList) {
            dates.add("'" + tmp.getsDate() + "'");
            sales.add(tmp.getsOrderAmount().floatValue());
        }

        res.addObject("storeId", storeId);
        res.addObject("storeList", storeList);
        res.addObject("searchScope", searchDay);
        res.addObject("orderAmount", orderAmount);
        res.addObject("orderAmountLimit", orderAmountLimit);
        res.addObject("goodsCount", goodsCount);
        res.addObject("dates", dates);
        res.addObject("sales", sales);

        return res;
    }

    /**
     * 商品分析
     * @param request
     * @return
     */
    @RequestMapping("goodsAnalysis")
    public ModelAndView goodsStat(HttpServletRequest request) {
        //查询所有店铺信息
        Store store = new Store();
        store.setPageSwitch(false);
        List<Store> storeList = storeMapper.findStoreByCondition(store);
        ModelAndView res = new ModelAndView("stat/goodsAnalysis");
        //查询条件
        String storeId = request.getParameter("storeId");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String startPrice = request.getParameter("startPrice");
        String endPrice = request.getParameter("endPrice");
        GoodsStatistical goodsStatistical = new GoodsStatistical();
        //改页处理用
        String start = request.getParameter("start");
        if (start != null && !"".equals(start)) {
            goodsStatistical.setStart(Integer.valueOf(start));
        }
        if (storeId != null && !"".equals(storeId)) {
            goodsStatistical.setStoreId(Integer.valueOf(storeId));
        }
        goodsStatistical.setStartDateStr(startDate);
        goodsStatistical.setEndDateStr(endDate);
        if (startPrice != null && !"".equals(startPrice)) {
            goodsStatistical.setStartPrice(new BigDecimal(startPrice));
        }
        if (endPrice != null && !"".equals(endPrice)) {
            goodsStatistical.setEndPrice(new BigDecimal(endPrice));
        }
        goodsStatistical.setResultList(goodsMapper.selectGoodsSales(goodsStatistical));
        res.addObject("storeList", storeList);
        res.addObject("page", goodsStatistical);
        return res;
    }

    /**
     * 商品导出
     * @param request
     * @return
     */
    @RequestMapping(value = "exportGoodsExcel")
    public String exportGoodsExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //查询条件
        String storeId = request.getParameter("storeId");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String startPrice = request.getParameter("startPrice");
        String endPrice = request.getParameter("endPrice");
        List exportData = new ArrayList<Map>();
        LinkedHashMap map = new LinkedHashMap();
        GoodsStatistical goodsStatistical = new GoodsStatistical();
        goodsStatistical.setPageSwitch(false);
        if (storeId != null && !"".equals(storeId)) {
            goodsStatistical.setStoreId(Integer.valueOf(storeId));
        }
        goodsStatistical.setStartDateStr(startDate);
        goodsStatistical.setEndDateStr(endDate);
        if (startPrice != null && !"".equals(startPrice)) {
            goodsStatistical.setStartPrice(new BigDecimal(startPrice));
        }
        if (endPrice != null && !"".equals(endPrice)) {
            goodsStatistical.setEndPrice(new BigDecimal(endPrice));
        }
        List<GoodsStatistical> list = goodsMapper.selectGoodsSales(goodsStatistical);
        for (GoodsStatistical gs : list) {
            Map row = new LinkedHashMap<String, String>();
            row.put("1", gs.getStoreName());
            row.put("2", gs.getGoodsName());
            row.put("3", gs.getGoodsPrice()!=null?gs.getGoodsPrice():"0.00");
            row.put("4", gs.getGoodsSerial()!=null?gs.getGoodsSerial():"其他");
            row.put("5", gs.getSellNum()!=null?gs.getSellNum():"0");
            row.put("6", gs.getSellSum()!=null?gs.getSellSum():"0.00");
            exportData.add(row);

        }
        map.put("1", "店铺");
        map.put("2", "商品名称");
        map.put("3", "当前售价");
        map.put("4", "渠道商");
        map.put("5", "购买数量");
        map.put("6", "销售金额");
        String fileName = "goods_statistical";
        String ctxPath = request.getSession().getServletContext().getRealPath("/") + "\\" + "temp\\";
        CsvUtil.createCSVFile(exportData, map, ctxPath, fileName);
        CsvUtil.downloadCSVFile(fileName + ".csv", ctxPath, request, response);
        return null;
    }

    /**
     * 会员消费
     * @param request
     * @return
     */
    @RequestMapping(value = "memberAnalysis")
    public ModelAndView memberConsumpStatistic(HttpServletRequest request){
        ModelAndView res = new ModelAndView("stat/memberAnalysis");
        //查询条件
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        MemberConsumptionStatisticVo memberConsumptionStatisticVo = new MemberConsumptionStatisticVo();
        //改页处理用
        String start = request.getParameter("start");
        if (start != null && !"".equals(start)) {
            memberConsumptionStatisticVo.setStart(Integer.valueOf(start));
        }
        memberConsumptionStatisticVo.setStartTimeStr(startTime);
        memberConsumptionStatisticVo.setEndTimeStr(endTime);
        List<MemberConsumptionStatisticVo> memberConsumptionStatisticVos = statOrderGoodsService.getMemberConsumpStatisticInfo(memberConsumptionStatisticVo);
        memberConsumptionStatisticVo.setResultList(memberConsumptionStatisticVos);
        res.addObject("page", memberConsumptionStatisticVo);
        return res;
    }

//    /**
//     * 行业分析
//     *
//     * @return
//     */
//    @RequestMapping("industryAnalysis")
//    public String industryAnalysis() {
//        return "";
//    }
//
//    /**
//     * 营销分析
//     *
//     * @return
//     */
//    @RequestMapping("marketAnalysis")
//    public String marketAnalysis() {
//        return "";
//    }
//
//    /**
//     * 销售分析
//     *
//     * @return
//     */
//    @RequestMapping("salesAnalysis")
//    public String salesAnalysis() {
//        return "";
//    }
//
//    /**
//     * 店铺分析
//     *
//     * @return
//     */
//    @RequestMapping("storeAnalysis")
//    public String storeAnalysis() {
//        return "";
//    }
//
//    /**
//     * 售后分析
//     *
//     * @return
//     */
//    @RequestMapping("aftermarketAnalysis")
//    public String aftermarketAnalysis() {
//        return "";
//    }
}
